package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.StudentRequestDTO;
import com.tianya.domain.entity.Clazz;
import com.tianya.domain.entity.Parent;
import com.tianya.domain.entity.Student;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.StudentVO;
import com.tianya.service.ClazzService;
import com.tianya.service.ParentService;
import com.tianya.service.StudentService;
import com.tianya.mapper.StudentMapper;
import com.tianya.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Part;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【student(学生表)】的数据库操作Service实现
* @createDate 2025-05-24 19:00:05
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{
    @Resource
    private UserService userService;

    @Resource
    private ParentService parentService;
    @Resource
    private ClazzService clazzService;
    @Override
    public void createStudent(StudentRequestDTO studentRequestDTO) {
        Student student = Student.builder()
                .name(studentRequestDTO.getName())
                .gender(studentRequestDTO.getGender())
                .age(studentRequestDTO.getAge())
                .clazzId(studentRequestDTO.getClazzId())
                .parentId(studentRequestDTO.getParentId())
                .build();
        save(student);
    }

    @Override
    public void editStudent(Long id ,Student student) {
        Student student0 = getById(id);
        student0.setName(student.getName());
        student0.setClazzId(student.getClazzId());
        student0.setParentId(student.getParentId());
        updateById(student0);
    }

    @Override
    public PageResultVO<StudentVO> listPage(Integer pageNum, Integer pageSize, StudentRequestDTO studentRequestDTO) {
        Page pageParam = new Page<>(pageNum, pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(studentRequestDTO.getName()!=null,Student::getName, studentRequestDTO.getName())
                .eq(studentRequestDTO.getGender()!=null,Student::getGender, studentRequestDTO.getGender())
                .eq(studentRequestDTO.getClazzId()!=null,Student::getClazzId, studentRequestDTO.getClazzId());
        Page<Student> pageResult = page(pageParam, queryWrapper);
        PageResultVO<StudentVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(pageResult.getTotal());
        List<StudentVO> studentVOList = new ArrayList<>();
        for (Student student : pageResult.getRecords()) {
            Long parentId = student.getParentId();
            Parent  parent = parentService.getById(parentId);
            Long userId = parent.getUserId();
            User user = userService.getById(userId);
            Long clazzId = student.getClazzId();
            Clazz clazz = clazzService.getById(clazzId);
            StudentVO studentVO = new StudentVO();
            studentVO.setPhone(user!= null?user.getPhone():"unKnown");
            studentVO.setParentName(user!=null?user.getName():"unKnown");
            studentVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
            BeanUtil.copyProperties(student, studentVO);
            studentVOList.add(studentVO);
        }
        pageVO.setRecords(studentVOList);
        return pageVO;
    }

    @Override
    public List<StudentVO> listStudent(Long id) {
        List<Student> students = list(new QueryWrapper<Student>().eq("clazz_id", id));
        List<StudentVO> studentVOList = new ArrayList<>();
        for (Student student : students) {
            Long parentId = student.getParentId();
            Parent  parent = parentService.getById(parentId);
            Long userId = parent.getUserId();
            User user = userService.getById(userId);
            StudentVO studentVO = new StudentVO();
            studentVO.setPhone(user!= null?user.getPhone():"unKnown");
            studentVO.setParentName(user!=null?user.getName():"unKnown");
            BeanUtil.copyProperties(student, studentVO);
            studentVOList.add(studentVO);
        }
        return studentVOList;
    }

    @Override
    public List<StudentVO> listStudentByHeaderTeacher(Long id) {
        Clazz clazz = clazzService.getOne(new QueryWrapper<Clazz>()
                .eq("head_teacher_id", id));
        return listStudent(clazz.getClazzId());
    }
}




